﻿/*
	Copyright 2009 Jason Nottingham

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.


    How to submit a patch:
    1. create or modify a test case (under 'tests') so that it fails to compile/link/run without 
       your change
    2. verify you have the latest sources (resync/merge)
    3. fix or add the missing functionality
    4. verify that your changes fixes the newly broken test case in #1 - it now builds and runs 
       C++ the same as the original C#
    5. create a Subversion-friendly diff or patch file of your changes (including the test cases)
    6. submit your patch at http://gonative.codeplex.com


*/
using System;
using System.Collections.Generic;
using System.IO;

namespace GoNative
{

    /// <summary>
    /// 
    /// </summary>
    class SolutionFile
    {
        private readonly string name;
        private readonly ProjectFile[] projects;

        public string Name
        {
            get { return name; }
        }

        public ProjectFile[] Projects
        {
            get { return projects; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        public SolutionFile(string fileName)
        {
            name = fileName;
            FileInfo fi = new FileInfo(fileName);

            if (!fi.Exists)
            {
                Console.WriteLine("Solution file [{0}] not found!", fileName);
                return;
            }

            List<ProjectFile> subprojects = new List<ProjectFile>();
            string[] lines = File.ReadAllLines(fileName);
            foreach (string line in lines)
            {
                if (line.StartsWith("Project("))
                {
                    string[] fields = line.Split(',');
                    if (fields.Length > 2)
                    {
                        string filename = fields[1].Replace('"', ' ').Trim();
                        string directoryName = fi.DirectoryName;
                        if (string.IsNullOrEmpty(directoryName))
                            directoryName = ".";
                        filename = Path.Combine(directoryName, filename);
                        if ( File.Exists(filename) )
                            subprojects.Add( new ProjectFile(filename) );
                    }
                }
            }
            projects = subprojects.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        internal void Process()
        {
            Console.WriteLine("Processing solution file {0}", name);
            if (projects != null)
                foreach (ProjectFile project in projects)
                    project.Process();
        }
    }
}
